@extends('layouts.layout')

@section('header')

    <title>添加地址</title>

    <link rel="stylesheet" type="text/css" href="{{URL::asset('static/css/addAddress.css')}}"/>
    <link rel="stylesheet" type="text/css" href="{{URL::asset('static/css/picker.css')}}"/>

@endsection

@section('content')
    <form class="form">
        <div class="formview clearfix">
            <span>收货人:</span>
            <input type="text" name="name" autocomplete="off" placeholder="请输入收货人名称" />
        </div>
        <div class="password formview">
            <span>手机号码:</span>
            <input type="text" name="mobile" autocomplete="off" placeholder="请输入手机号" />
        </div>
        <div class="password formview" onclick="picker()">
            <span>收货地址:</span>
            <input type="text" name="area" readonly autocomplete="off" placeholder="请选择地址" />
        </div>
        <div class="password formview">
            <span>详细地址:</span>
            <input type="text" name="address" autocomplete="off" placeholder="请输入详细地址" />
        </div>
    </form>

    <a href="javascript:;" class="add_address">保存</a>

@endsection

@section('script')
    <script src="{{URL::asset('static/js/picker.js')}}" type="text/javascript" charset="utf-8"></script>
	<script src="{{URL::asset('static/js/citydata.js')}}" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript">

        function picker() {
            new Picker({
                title: "请滑动屏幕选择", //标题(可选)
                defaultValue: $("input[name=area]").val(), //默认值-多个以空格分开（可选）
                data: cityData, //数据(必传)
                type: 3,
                keys: {
                    id: "code",
                    value: "name",
                    childData: "level", //最多3级联动
                }, //数组内的键名称(必传，id、text、data)
                callBack: function (val) {
                    //回调函数（val为选择的值）
                    let city = val.split(" ");
                    if(city[0] != ""){
                        $("input[name=area]").val(val)
                    }
                },
            });
        }
        $(function () {
            $(function(){
                var type = sessionStorage.getItem('addressType')
                if(type == "change"){ // 修改地址
                    $(document).attr("title", "修改地址");
                    var address = JSON.parse(sessionStorage.getItem('changeAddress'))
                    $('input[name="name"]').val(address.name)
                    $('input[name="mobile"]').val(address.mobile)
                    $('input[name="area"]').val(address.area)
                    $('input[name="address"]').val(address.address)
                    function addressFnc (data){
                        commonAjax({type:"post",url:'/user/saveAddress',data},function(msg){

                            setTimeout(function(){
                                layer.open({
                                    content: '修改成功!'
                                    ,skin: 'msg'
                                    ,time: 2
                                    ,end:function(){
                                        window.history.go(-1)
                                    }
                                  });
                            })

                        })
                    }
                }else{
                    function addressFnc (data){
                        commonAjax({type:"post",url:'/user/createAddress',data},function(msg){

                            setTimeout(function(){
                                layer.open({
                                    content: '添加成功!'
                                    ,skin: 'msg'
                                    ,time: 2
                                    ,end:function(){
                                        window.history.go(-1)
                                    }
                                  });
                            })

                        })
                    }
                }
                // 添加地址
                $(".add_address").click(function(){
                    var data = $(".form").serialize();
                    data =toJson(decodeURIComponent(data,true));
                    if(type == "change"){
                        data['id'] = address.id
                    }
                    if (!data.name.trim()) {
						layer.open({
						    content: '请填写收货人!'
						    ,skin: 'msg'
						    ,time: 1
						  });
                    	return false;
                    }else if (!data.mobile.trim()) {
						layer.open({
						    content: '请填写手机号!'
						    ,skin: 'msg'
						    ,time: 1
						  });
                    	return false;
                    }else if (!(/^(((13[0-9]{1})|(16[0-9]{1})|(19[0-9]{1})|(15([0-9]{1}))|(18[0-9]{1})|(17+(0|1|2|4|5|3|6|7|8))|(14+(5|6|7|8|9)))+\d{8})$/
                            .test(data.mobile))) {
						layer.open({
						    content: '手机号格式不正确!'
						    ,skin: 'msg'
						    ,time: 1
						  });
                        return false;
                    }else if (!data.area.trim()) {
						layer.open({
						    content: '请选择收货地址!'
						    ,skin: 'msg'
						    ,time: 1
						  });
                    	return false;
                    }else if (!data.address.trim()) {
						layer.open({
						    content: '请填写详细地址!'
						    ,skin: 'msg'
						    ,time: 1
						  });
                    	return false;
                    }
                    addressFnc(data)
                    return false;
                })

            })


            //		将字符串转为json
            function toJson(data) {
            	data = data.split('&')
            	var json = {}
            	$.each(data, function(index, value) {
            		var arr = value.split('=');
            		json[arr[0]] = arr[1]
            	});
            	return json
            }
        })
    </script>

@endsection
